package app

import (
	"barge/pkg/acp"
	"barge/pkg/manager"
	"github.com/rs/zerolog/log"
)

var SupportedArchitectures = []string{"amd64", "arm64"}

type RegistryOption struct {
	manager.Registry
	Acp     string
	Token   string
	Cluster string
}

func (m *ConfigManager) SetRegistry(opt *RegistryOption) {
	if opt.Acp == "" {
		m.Config.SetRegistry(&opt.Registry)
		return
	}
	acpOpt := &acp.Option{
		Name:    opt.Name,
		Host:    opt.Acp,
		Token:   opt.Token,
		Cluster: opt.Cluster,
	}
	controller := acp.NewController(acpOpt)
	registry, err := controller.GetRegistryInfo()
	if err != nil {
		log.Err(err).Send()
		return
	}
	if len(opt.Architectures) > 0 {
		registry.Architectures = opt.Architectures
	} else {
		registry.Architectures, err = controller.GetArchitectures(SupportedArchitectures)
		if err != nil {
			log.Err(err).Send()
			return
		}
	}

	registry.Name = opt.Name
	m.Config.SetRegistry(registry)
}
